package com.onepice.mlp.core.common;

import com.onepice.mlp.core.dto.RequestDto;
import com.onepice.mlp.utils.commom.ConvUtil;
import com.onepice.mlp.utils.commom.PageUtil;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @Author: fire
 * @Time: 2022/3/22 00:39
 **/
public class QueryUtil {
    private PageUtil page;
    private Root<?> root;
    private CriteriaQuery<?> criteriaQuery;
    private CriteriaBuilder criteriaBuilder;

    @Getter
    private List<Predicate> predicateList;

    /**
     * 根据请求参数列表获取分页和排序条件
     *
     * @param requestDto
     * @return
     */
    public PageUtil getPage(RequestDto requestDto) {
        if (ConvUtil.convToString(requestDto.getOrderString()).equals("")) {
            page = new PageUtil(requestDto.getPageNumber(), requestDto.getPageSize());
        } else {
            page = new PageUtil(requestDto.getPageNumber(), requestDto.getPageSize(), requestDto.getOrderString(), requestDto.getOrderType());
        }
        return page;
    }

    /**
     * 设置多条件初始化参数
     *
     * @param root            查询实体类型
     * @param criteriaQuery   多表查询体
     * @param criteriaBuilder 条件拼接工具
     * @return
     */
    public void init(Root<?> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
        this.root = root;
        this.criteriaQuery = criteriaQuery;
        this.criteriaBuilder = criteriaBuilder;
        this.predicateList = new ArrayList<>();
    }

    /**
     * =条件拼接
     *
     * @param fieldName  字段名称
     * @param fieldValue 字段匹配值
     * @return
     */
    public void equal(String fieldName, Integer fieldValue) {
        if (!ConvUtil.convToString(fieldName).equals("")  && fieldValue != null) {
            predicateList.add(criteriaBuilder.equal(root.get(fieldName), fieldValue));
        }
    }

    /**
     * like条件拼接
     *
     * @param fieldName  字段名称
     * @param fieldValue 字段匹配值
     * @return
     */
    public void like(String fieldName, String fieldValue) {
        if (!ConvUtil.convToString(fieldName).equals("") && ConvUtil.convToString(fieldValue).equals("")) {
            predicateList.add(criteriaBuilder.equal(root.get(fieldName), "%" + fieldValue + "%"));
        }
    }

    /**
     * between条件拼接
     *
     * @param fieldName  字段名称
     * @param startValue 范围开始值
     * @param endValue   范围结束值
     * @return
     */
    public void between(String fieldName, Date startValue, Date endValue) {
        if (!ConvUtil.convToString(fieldName).equals("") && startValue != null && endValue != null) {
            predicateList.add(criteriaBuilder.between(root.get(fieldName), startValue, endValue));
        }
    }

    /**
     * like条件拼接
     *
     * @param fieldName 字段名称
     * @param fieldList 字段包含列表
     * @return
     */
    public void in(String fieldName, List<?> fieldList) {
        if (!ConvUtil.convToString(fieldName).equals("") && fieldList.size() > 0) {
            CriteriaBuilder.In in = criteriaBuilder.in(root.get(fieldName));
            for (Object fieldValue : fieldList) {
                in.value(fieldValue);
            }
            predicateList.add(in);
        }
    }

    /**
     * 公共参数拼接方法
     *
     * @param query      条件拼工具实例（需要已经初始化的）
     * @param requestDto 公共请求参数体
     */
    public static void commonQuery(QueryUtil query, RequestDto requestDto) {
        query.like("singerName", requestDto.getSingerName());
        query.like("songName", requestDto.getSongName());
        query.like("albumName", requestDto.getAlbumName());
        query.equal("styleType", requestDto.getStyleType());
    }
}
